home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 213_01 / dior.c < prev    next >
Text File  |  1980-01-01  |  15KB  |  636 lines

  1. /* DIOR.C         VERS:- 01.00  DATE:- 09/26/86  TIME:- 09:36:32 PM */
  2. /*
  3. %CC1 $1.C -X
  4. */
  5. /* 
  6. Description:
  7.  
  8. IO redirection library from BDS C and Van Nuys Toolkit, 
  9. by Eugene H. Mallory.
  10.  
  11. Minor modification:
  12.     avoid CR-CR pair before LF by patch in putchar() of DIOR.C and by 
  13.         adding declaration of _putc_last in DIO.H;
  14.  
  15. By J.A. Rupley, Tucson, Arizona
  16. Coded for BDS C compiler, version 1.50a
  17. */
  18. /*********************************************************************
  19. *                               DIO                                  *
  20. *                Written by Gene Mallory  12/24/83                   *
  21. **********************************************************************
  22. *                  COPYRIGHT 1983 EUGENE H. MALLORY                  *
  23. *********************************************************************/
  24. /*
  25.     Redirection can take the following forms:
  26.     
  27.         > fid    Output redirection to file
  28.         < fid    Input rediredtion from file
  29.         + fid    Output redirection to file and screen
  30.         ~ fid    Output redirection to file and printer
  31.         >> fid    Append to file
  32.         +> fid    Append to file and output to screen
  33.         ~> fid    Append to file and output to printer
  34.         > LST:    Output to LST device
  35.         >> LST:    As above
  36.         + LST:    Output to LST device and screen
  37.         > LP1:    Output to LP1 (IOBYTE = 0x80)
  38.         >> LP1:    As above
  39.         + LP1:    As above and to screen
  40.         > LP2:    Output to LP2 (IOBYTE = 0xC0)
  41.         >> LP2:    As above
  42.         + LP2:    As above and to screen
  43.         > OUT:    Output to PUNCH DEVICE
  44.         >> OUT:    As above
  45.         + OUT:    As above and to screen
  46.         ~ OUT:    As above and to printer
  47.         < IN:    Input from READER device
  48.         
  49.         | prog    Pipe output to next program
  50.         +| prog    As above and to screen
  51.         ~| prog    As above and to printer
  52.     
  53.     Redirection and pipe character may be separated from
  54.     the file or program name by white space or not.
  55.     
  56.     File specifications may be complete with user number (##) and 
  57.     disk name (d) in the form:
  58.     
  59.             ##/d:name
  60.     
  61.     Pipe specification will be first tried as is, if the file 
  62.     does not exist, disk A, user 0 will be tried using the same 
  63.     name.
  64.     
  65.     Control C will abort program and submit file execution.
  66.     When inputing from keyboard use Control C except at
  67.     column 1. (CP/M catches it there.)  If an old version of
  68.     the output file existed, it will be recovered by Control C.
  69.     
  70.     It is permissable to input and output from the same file,
  71.     BUT the input specification must be first and the output
  72.     specification must be second!  otherwise the order and 
  73.     location of the redirection specifications is not
  74.     significant.
  75.     
  76.         
  77.     
  78.     
  79.     Thus, a C program using redirection has the following form:
  80.  
  81.         #include "bdscio.h"        /* standard header file    */
  82.         #include "dio.h"        /* directed I/O header    */
  83.         
  84.         ...                /* other externals, if any */
  85.  
  86.         main(argc,argv)
  87.         char **argv;
  88.         BEGIN
  89.             ...            /* declarations             */
  90.             dioinit(&argc,argv)    /* initialize redirection    */
  91.             wildexp(&argc,&argv)/* wildexp goes here if used */
  92.             ...            /* body of program         */
  93.             dioflush();        /* Must exit here to close   */
  94.         END
  95. */
  96. /*
  97.     NOTES:
  98.  
  99.     1. Redirection and pipes work only for TEXT. This mechanism 
  100.     should not be used for binary data.
  101.  
  102.     2. Use CLINK FOO DIO or L2 FOO DIO to force 
  103.     linking with all the dio routines before the DEFFs are 
  104.     searched.
  105.     
  106.     3. Library routines GETCHAR, PUTCHAR, PRINTF, PUTS are
  107.     caught and redirected.
  108.     
  109.     4. New routines TYPEF, TYPECHAR FORCE OUTPUT TO 
  110.     SCREEN.
  111.     
  112.     5. New routine ERROR, may be used like TYPEF to 
  113.     output a message, close out the DIO operations
  114.     and exit.
  115.     
  116.  
  117. */
  118.  
  119. #include "bdscio.h"
  120. #include "dio.h"
  121.  
  122. /*********************************************************************
  123. *                           DIOINIT                                  *
  124. *********************************************************************/
  125. dioinit(argcptr, argvect)
  126. int *argcptr;
  127. char **argvect;
  128. {
  129.     int i, c0, c1;
  130.     _outptr = STDERR;
  131.     _conline = MAXLINE;
  132.     _conbufp = _conbuf;
  133.     _conbuf[0] = 0;
  134.     _iobyte = bdos(7, 0);        /* save iobyte */
  135.     _screenflag = _pipeflag = _dioinflag = FALSE;
  136.     _printerflag = _appendflag = _rdrflag = FALSE;
  137.  
  138.     for (i = 1; i < *argcptr;)
  139.     {
  140.         c0 = argvect[i][0];
  141.         c1 = argvect[i][1];
  142.         if (c0 == '+')
  143.             _screenflag = TRUE;
  144.         if (c0 == '~')
  145.             _printerflag = TRUE;
  146.  
  147.         if ((c0 == '+' || c0 == '~') && c1 == '|')
  148.         {
  149.             strshift(argvect[i], 1);
  150.             _dio_pipe(i, argcptr, argvect);
  151.         }
  152.         else
  153.             if ((c0 == '+' || c0 == '~') && c1 == '>')
  154.             {
  155.                 _dioappend(i, argcptr, argvect);
  156.             }
  157.         else
  158.             if (c0 == '>' && c1 == '>')
  159.         {
  160.             _dioappend(i, argcptr, argvect);
  161.         }
  162.         else
  163.             if (c0 == '>' || (c0 == '+' || c0 == '~'))
  164.         {
  165.             _dioout(i, argcptr, argvect);
  166.         }
  167.         else
  168.             if (c0 == '<')
  169.         {
  170.             _dioin(i, argcptr, argvect);
  171.         }
  172.         else
  173.             if (c0 == '|')
  174.         {
  175.             _dio_pipe(i, argcptr, argvect);
  176.         }
  177.         else
  178.         {
  179.             i++;
  180.         }
  181.     }
  182. }
  183.  
  184. /*********************************************************************
  185. *                           _DIOOUT                                  *
  186. *********************************************************************/
  187. _dioout(n, argcptr, argvect)
  188. int *argcptr, n;
  189. char **argvect;
  190. {
  191.     char backupfile[20];
  192.     _outptr = _dobuf;
  193.  
  194.     if (argvect[n][0] == '+')
  195.         _screenflag = TRUE;
  196.  
  197.     if (strlen(argvect[n]) == 1)
  198.         shiftarg(n, argcptr, argvect);
  199.     else
  200.         strshift(argvect[n], 1);
  201.  
  202.     if (!strcmp(argvect[n], "LST:"))
  203.         _outptr = STDLST;
  204.     else
  205.         if (!strcmp(argvect[n], "OUT:"))
  206.         _outptr = STDPUN;
  207.     else
  208.         if (!strcmp(argvect[n], "LP1:"))
  209.     {
  210.         _outptr = STDLST;
  211.         bdos(8, (_iobyte & 0x3f) | 0x80);
  212.     }
  213.     else
  214.         if (!strcmp(argvect[n], "LP2:"))
  215.     {
  216.         _outptr = STDLST;
  217.         bdos(8, (_iobyte & 0x3f) | 0xC0);
  218.     }
  219.     else
  220.         {
  221.         strcpy(_oldfile, argvect[n]);
  222.         getprefix(backupfile, _oldfile);
  223.         strcat(backupfile, "BACKUP.$$$");
  224.         unlink(backupfile);
  225.         if (DIOIN && (strcmp(argvect[n], _infile) == 0))
  226.         {
  227.             fclose(_dibuf);
  228.             if (rename(argvect[n], backupfile) == -1)
  229.             {
  230.                 fprintf(STDERR, "DIO: Failed to rename %s to %s.\n",
  231.                 argvect[n], backupfile);
  232.                 dioexit();
  233.             }
  234.             if (fopen(backupfile, _dibuf) == ERROR)
  235.             {
  236.                 fprintf(STDERR, "DIO: Unable to open file %s for input.\n"
  237.                     , backupfile);
  238.                 fprintf(STDERR, "DIO: Error caused by %s.\n", errmsg(errno()));
  239.                 dioexit();
  240.             }
  241.         }
  242.         else
  243.             {
  244.             rename(argvect[n], backupfile);
  245.         }
  246.         if (fcreat(argvect[n], _dobuf) == ERROR)
  247.         {
  248.             fprintf(STDERR, "DIO: Unable to open file %s for output.\n"
  249.                 , argvect[n]);
  250.             fprintf(STDERR, "DIO: Error caused by %s.\n", errmsg(errno()));
  251.             dioexit();
  252.         }
  253.     }
  254.     shiftarg(n, argcptr, argvect);
  255. }
  256.  
  257. /*********************************************************************
  258. *                           _DIO_PIPE
  259. *********************************************************************/
  260. _dio_pipe(n, argcptr, argvect)
  261. int *argcptr, n;
  262. char **argvect;
  263. {
  264.     if (strlen(argvect[n]) == 1)
  265.         shiftarg(n, argcptr, argvect);
  266.     else
  267.         strshift(argvect[n], 1);
  268.  
  269.     _pipeflag = 1;
  270.     _outptr = _dobuf;
  271.  
  272.     _pargv = &argvect[n];
  273.     _pargc = *argcptr - n;
  274.     *argcptr = n;
  275.  
  276.     if (fcreat("TEMPOUT.$$$", _dobuf) == ERROR)
  277.     {
  278.         fprintf(STDERR, "DIO: Unable to open file TEMPOUT.$$$ for pipe.\n");
  279.         fprintf(STDERR, "DIO: Error caused by %s.\n", errmsg(errno()));
  280.         dioexit();
  281.     }
  282. }
  283.  
  284. /*********************************************************************
  285. *                           _DIOAPPEND
  286. *********************************************************************/
  287. _dioappend(n, argcptr, argvect)
  288. int *argcptr, n;
  289. char **argvect;
  290. {
  291.     _appendflag = TRUE;
  292.     _outptr = _dobuf;
  293.  
  294.     if (argvect[n][0] == '+')
  295.         _screenflag = TRUE;
  296.  
  297.     if (strlen(argvect[n]) == 2)
  298.         shiftarg(n, argcptr, argvect);
  299.     else
  300.         strshift(argvect[n], 2);
  301.  
  302.     if (!strcmp(argvect[n], "LST:"))
  303.         _outptr = STDLST;
  304.     else
  305.         if (!strcmp(argvect[n], "OUT:"))
  306.         _outptr = STDPUN;
  307.     else
  308.         if (!strcmp(argvect[n], "LP1:"))
  309.     {
  310.         _outptr = STDLST;
  311.         bdos(8, (_iobyte & 0x3f) | 0x80);
  312.     }
  313.     else
  314.         if (!strcmp